package com.lex.activitytext;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/6/29.
 */
public class CaseInfoEntity{
    private String nu;
    private String com;
    private String condition;
    private String status;
    private String state;
    private String message;
    private String ischeck;
    private List<LogisticsInfoEntity> data;

    private static final String KEY_NU = "nu";
    private static final String KEY_COM = "com";
    private static final String KEY_CONDITION = "condition";
    private static final String KEY_STATUS = "status";
    private static final String KEY_STATE = "state";
    private static final String KEY_MESSAGE = "message";
    private static final String KEY_ISCHECK = "ischeck";
    private static final String KEY_DATA = "data";

    @Override
    public String toString() {
        return "CaseInfoEntity{" +
                "nu='" + nu + '\'' +
                ", com='" + com + '\'' +
                ", condition='" + condition + '\'' +
                ", status='" + status + '\'' +
                ", state='" + state + '\'' +
                ", message='" + message + '\'' +
                ", ischeck='" + ischeck + '\'' +
                ", data=" + data +
                '}';
    }

    public static CaseInfoEntity personFromJsonStr(String jsonStr) {
        CaseInfoEntity caseInfoEntity = new CaseInfoEntity();
        try {
            JSONObject jsonObject = new JSONObject(jsonStr);
            caseInfoEntity.nu = jsonObject.getString(KEY_NU);
            caseInfoEntity.com = jsonObject.optString(KEY_COM);
            caseInfoEntity.condition = jsonObject.optString(KEY_CONDITION);
            caseInfoEntity.status = jsonObject.optString(KEY_STATUS);
            caseInfoEntity.state = jsonObject.optString(KEY_STATE);
            caseInfoEntity.message = jsonObject.optString(KEY_MESSAGE);
            caseInfoEntity.ischeck = jsonObject.optString(KEY_ISCHECK);
            JSONArray jsonArray = jsonObject.optJSONArray(KEY_DATA);
            if (jsonArray.length() > 0) {
                List<LogisticsInfoEntity> logisticsList = new ArrayList<>();
                for (int i = 0; i < jsonArray.length(); i++) {
                    logisticsList.add(LogisticsInfoEntity.parseFromJsonObj(jsonArray.getJSONObject(i)));
                }
                caseInfoEntity.data = logisticsList;
            }
        } catch (JSONException e) {
            return caseInfoEntity;
        }
        return caseInfoEntity;
    }

    static class LogisticsInfoEntity {
        private String time;
        private String context;
        private String ftime;

        @Override
        public String toString() {
            return "LogisticsInfoEntity{" +
                    "time='" + time + '\'' +
                    ", context='" + context + '\'' +
                    ", ftime='" + ftime + '\'' +
                    '}';
        }

        private static final String KEY_TIME = "time";
        private static final String KEY_CONTEXT = "context";
        private static final String KEY_FTIME = "ftime";

        private static LogisticsInfoEntity parseFromJsonObj(JSONObject jsonObject) {
            LogisticsInfoEntity logisticsInfoEntity = new LogisticsInfoEntity();
            try {
                logisticsInfoEntity.time = jsonObject.getString(KEY_TIME);
                logisticsInfoEntity.context = jsonObject.getString(KEY_CONTEXT);
                logisticsInfoEntity.ftime = jsonObject.getString(KEY_FTIME);
            } catch (JSONException e) {
                Log.e("lex", "解析JSON到LogisticsInfoEntity对象时发生错误");
                return null;
            }
            return logisticsInfoEntity;
        }
    }
}